package com.vhuan.self_discipline_service.config;

import com.vhuan.self_discipline_service.interceptor.JWTInterceptor;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Arrays;
import java.util.List;

@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
    @Resource
    private JWTInterceptor jwtInterceptor;

    @Value("${jwt.interceptor.exclude-urls}")
    private String excludePathsConfig;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        if (jwtInterceptor == null) {
            throw new IllegalStateException("JWTInterceptor is not properly injected.");
        }

        List<String> excludePaths = Arrays.asList(excludePathsConfig.split(","));
        registry.addInterceptor(jwtInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(excludePaths);
    }
}
